#!/usr/bin/env python
#_*_coding:utf-8 _*_
__author__ = 'silence'
import sys,time,datetime
import file_operate

#取款
def enchashment(login_user):
    user_info = file_operate.file_read("user_info.pkl")
    log_info = file_operate.read_log("Billing_details.pkl")
    total_money = user_info[login_user][3]
    consumption_money = user_info[login_user][4]
    avilable_money = total_money - consumption_money
    print u"您能用的最大额度为\033[31;0m%.2f￥\033[0m" %avilable_money
    print u"您能取现的最大金额为\033[31;0m%.2f\033[0m" %(avilable_money/2)
    takeout_money = raw_input(u"请输入取现金额:").strip()
    if takeout_money.isdigit():
        takeout_money = float(takeout_money)
        if takeout_money <= (avilable_money/2):
            print u"请稍候,正在为您出钞....."
            time.sleep(1)
            tax_money = float(takeout_money*0.05)
            consumption_money = float(takeout_money*(1+0.05) + consumption_money)
            user_info[login_user][4] = consumption_money
            now_datetime = datetime.datetime.now()
            log_info[now_datetime] = {login_user:u"%s %s,\033[31;0m取出 %.2f￥\033[0m现金,扣除\033[31;0m%.2f\033[0m￥手续费" %(now_datetime,login_user,takeout_money,tax_money)}
            print u"%s %s,\033[31;0m取出 %.2f￥\033[0m现金,扣除\033[31;0m%.2f\033[0m￥手续费" %(now_datetime,login_user,takeout_money,tax_money)
            #print log_info
            #print user_info
            file_operate.write_log(log_info,"Billing_details.pkl")
            file_operate.file_write(user_info,"user_info.pkl")
            return 0
        else:
            return 1
    else:
        return -1

#转账
def transfer_accounts(login_user):
    user_info = file_operate.file_read("user_info.pkl")
    log_info = file_operate.read_log("Billing_details.pkl")
    total_money = user_info[login_user][3]
    consumption_money = user_info[login_user][4]
    avilable_money = total_money - consumption_money
    print u"您你能使用的最大金额为\033[31;0m%.2f￥\033[0m" %avilable_money
    transfer_user = raw_input(u"请输入转入用户:").strip()
    transfer_money = raw_input(u"请输入转账金额:").strip()
    if transfer_user in user_info:
        if transfer_money.isdigit() and float(transfer_money) <=avilable_money:
            transfer_money = float(transfer_money)
            consumption_money = float(consumption_money + transfer_money)
            user_info[login_user][4] = consumption_money
            user_info[transfer_user][4] -= transfer_money
            now_datetime = datetime.datetime.now()
            log_info[now_datetime] = {login_user:u"%s %s \033[31;0m转出 %.2f￥ 到 %s\033[0m"%(now_datetime,login_user,transfer_money,transfer_user),transfer_user:u"%s \033[31;0m%s 转入 %.2f￥ 从 %s\033[0m"%(now_datetime,transfer_user,transfer_money,login_user)}
            print u"%s \033[31;0m%s 转出 %.2f￥ 到 %s\033[0m"%(now_datetime,login_user,transfer_money,transfer_user)
            #print log_info
            #print user_info
            file_operate.write_log(log_info,"Billing_details.pkl")
            file_operate.file_write(user_info,"user_info.pkl")
            return 0
        else:
            return 1
    else:
        return -1

#查询日志
def show_details(login_user,now_month):
    log_info = file_operate.read_log("Billing_details.pkl")
    #now_month = datetime.datetime.now().month
    for log_date in log_info.keys():
        if now_month == log_date.month:
            if login_user in log_info[log_date]:
                print log_info[log_date][login_user]
            else:
                print u"无查询日志"
        else:
            print u"无查询日志"

#购物调用模块
def shop(login_user):
    user_info = file_operate.file_read("user_info.pkl")
    total_money = user_info[login_user][3]
    consumption_money = user_info[login_user][4]
    avilable_money = total_money - consumption_money
    print u"您你能使用的最大金额为\033[31;0m%.2f￥\033[0m" %avilable_money
    shop_menu = [
            [u"茶杯",88],
            [u"电脑",5500],
            [u"电脑包",300],
            [u"自行车",3500],
            [u"茶叶",1000],
            [u"红牛",10]
        ]
    back_list = shopping(avilable_money,shop_menu,login_user)
    avilable_money = float(back_list)
    consumption_money = float(total_money - avilable_money)
    user_info[login_user][4] = consumption_money
    file_operate.file_write(user_info,"user_info.pkl")
    return True

#购物打印模块
def shopping(avilable_money,shop_menu,login_user):
    log_info = file_operate.read_log("Billing_details.pkl")
    salary = avilable_money
    while True:
        print u"编号\t商品名称\t\t商品价格"
        for index,p in enumerate(shop_menu):
            print index, "-->"+p[0]+"\t\t",p[1]

        shop_choice = raw_input(u"请选择商品编号(quit退出):").strip()
        if shop_choice.isdigit():
            shop_choice = int(shop_choice)
            p_price = shop_menu[shop_choice][1]
            if p_price < salary:
                salary -= p_price
                now_datetime = datetime.datetime.now()
                log_info[now_datetime] = {login_user:u"%s %s 够买了 \033[31;1m%s\033[0m,价格为\033[31;1m%.2f￥\033[0m" %(now_datetime,login_user,shop_menu[shop_choice][0],shop_menu[shop_choice][1])}
                print u"够买了 \033[31;1m%s\033[0m, 你当前余额为 \033[31;1m%s\033[0m" %(shop_menu[shop_choice][0],salary)
                file_operate.file_write(log_info,"Billing_details.pkl")
            else:
                print u"\033[41;1m余额不足, 请选择其他商品\033[0m"
        elif shop_choice == 'quit':
            return salary


if __name__ == '__main__':
    print u"本模块调用"
    #encha_status = enchashment("zhangduanyang")
    #print encha_status
    #transfer_accounts("zhangduanyang")
    #now_month = datetime.datetime.now().month -1
    #now_month = datetime.datetime.now().month
    #print now_month
    #show_details("zhangduanyang",now_month)
    #shop("zhangduanyang")
